package com.cskaoyan.Homework24;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(5678);
        Socket socket1 = socket.accept();
        System.out.println(socket1.getRemoteSocketAddress() + "已建立");
        InputStream in = socket1.getInputStream();
//        FileOutputStream out = new FileOutputStream("a.txt");
        byte[] buf = new byte[1024];
        int readCount;
        while ((readCount = in.read(buf))!=-1){
//           out.write(buf,0,readCount);
            String msg = new String(buf, 0, readCount);
            if ("end".equals(msg)){
                break;
            }
            System.out.println(msg);
        }
//        out.close();
        OutputStream out = socket1.getOutputStream();
        out.write("FIN".getBytes());
        socket.close();
    }
}
